package com.cci.kangdao.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cci.kangdao.service.WorkOrderListService;
import com.cci.kangdao.service.impl.SMSEnum;
import com.cci.kangdao.utilTool.Dictionaries;
import com.cci.kangdao.utilTool.ParameterTool;

@Controller
public class WorkOrderList {

	private Logger log = Logger.getLogger(this.getClass().getName());

	@Autowired
	private WorkOrderListService workOrderListService;

	/**
	 * 获取工单收藏列表
	 * @param request
	 * @param response
	 * @return
	 * @throws JSONException
	 */
	@RequestMapping("/getCollectionList.aspx")
	public ModelAndView getCollectionList(HttpServletRequest request,HttpServletResponse response) throws JSONException {
		JSONObject obj = new JSONObject();
		try {
			String eAppVersion = request.getHeader("EAppVersion");
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			map.put("eAppVersion", eAppVersion);
			return workOrderListService.getCollectionList(map);
		} catch (Exception e) {
			// TODO: handle exception
			//未知错误
			obj.put("status", SMSEnum.type99.getIndex());
			obj.put("msg", SMSEnum.type99.getName());
			obj.put("data", "");
			//返回客户
			ParameterTool.writeResponse(response, obj.toString());
			obj = null;
			return null;
		}
	}

	/**
	 * 设置  收藏、取消收藏 工单
	 * @param request
	 * @param response
	 * @return
	 * @throws JSONException
	 */
	@RequestMapping("/setUpCollection.aspx")
	public ModelAndView setUpCollection(HttpServletRequest request,HttpServletResponse response) throws JSONException {
		JSONObject obj = new JSONObject();
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			return workOrderListService.setUpCollection(map);
		} catch (Exception e) {
			// TODO: handle exception
			// TODO: handle exception
			//未知错误
			obj.put("status", SMSEnum.type99.getIndex());
			obj.put("msg", SMSEnum.type99.getName());
			obj.put("data", "");
			//返回客户
			ParameterTool.writeResponse(response, obj.toString());
			obj = null;
			return null;
		}
	}

	/**
	 * 获取工单列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getOrderList.aspx")
	public ModelAndView getOrderList(HttpServletRequest request,HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		try {
			String eAppVersion = request.getHeader("EAppVersion");
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			//首先判断必填参数是否齐全
			if(map.containsKey("OrderType") && map.containsKey("RoleType") && map.containsKey("UserId")) {
				map.put("request", request);
				map.put("response", response);
				map.put("eAppVersion", eAppVersion);
				//继续判断是什么用户查询列表
				String RoleType = map.get("RoleType").toString();
				if(RoleType.equals(String.valueOf(Dictionaries.roleType_01.getIndex()))) {
					//用户查询
					return workOrderListService.getCustomerOrderList(map);
				}else if(RoleType.equals(String.valueOf(Dictionaries.roleType_02.getIndex()))) {
					//服务技师
					return workOrderListService.getTechnicianOrderList(map);
				}else if(RoleType.equals(String.valueOf(Dictionaries.roleType_03.getIndex()))) {
					//服务站长
					return workOrderListService.getStationmasterOrderList(map);
				}else if(RoleType.equals(String.valueOf(Dictionaries.roleType_04.getIndex()))
				|| RoleType.equals(String.valueOf(Dictionaries.roleType_12.getIndex()))) {
					//dcec和xcec服务经理   ccec 400
					return workOrderListService.getManagerOrderList(map);
				}else if(RoleType.equals(String.valueOf(Dictionaries.roleType_05.getIndex()))
				|| RoleType.equals(String.valueOf(Dictionaries.roleType_10.getIndex()))
				|| RoleType.equals(String.valueOf(Dictionaries.roleType_11.getIndex()))
				|| RoleType.equals(String.valueOf(Dictionaries.roleType_13.getIndex()))
                || RoleType.equals(String.valueOf(Dictionaries.roleType_15.getIndex()))
				|| RoleType.equals(String.valueOf(Dictionaries.roleType_17.getIndex()))) {
					//dcec客户服务经理，xcec的服务总监，CCEC查询权限，400主管，DFSE调用一样的方法，都是能看到本渠道下所有工单
					ModelAndView cusManagerOrderList = workOrderListService.getCusManagerOrderList(map);
					return cusManagerOrderList;
				}else if(RoleType.equals(String.valueOf(Dictionaries.roleType_06.getIndex()))
				|| RoleType.equals(String.valueOf(Dictionaries.roleType_08.getIndex()))
				|| RoleType.equals(String.valueOf(Dictionaries.roleType_09.getIndex()))
				|| RoleType.equals(String.valueOf(Dictionaries.roleType_14.getIndex()))) {
					// dcec商务处主任和xcec大区经理，区域主任,ccec委托管理中心调用一样的方法
					return workOrderListService.getDepartmentOrderList(map);
				}else if(RoleType.equals(String.valueOf(Dictionaries.roleType_07.getIndex()))) {
					// 区域工程师(DBU)
					ModelAndView districtEngineerOrderList = workOrderListService.getDistrictEngineerOrderList(map);
					return districtEngineerOrderList;
				}else if(RoleType.equals(String.valueOf(Dictionaries.roleType_16.getIndex()))) {
                    // 服务工单经理(DBU)
					ModelAndView workOrderManagerOrderList = workOrderListService.getWorkOrderManagerOrderList(map);
					return workOrderManagerOrderList;
                }else {
					//参数非字典项
					JSONObject jb = new JSONObject();
					jb.put("functions", new ArrayList());
					obj.put("status", SMSEnum.type0.getIndex());
					obj.put("msg", SMSEnum.type0.getName());
					obj.put("data", jb);
					//返回客户
					ParameterTool.writeResponse(response, obj.toString());
					obj = null;
					return null;
				}
			}else {
				//缺少必要参数
				obj.put("status", SMSEnum.type17.getIndex());
				obj.put("msg", SMSEnum.type17.getName());
				obj.put("data", "");
				//返回客户
				ParameterTool.writeResponse(response, obj.toString());
				obj = null;
				return null;
			}
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			// TODO: handle exception
			ParameterTool.writeErrorResponse(response);
			return null;
		}
	}


	//外部系统终端用户获取工单详情    不拦截
	@RequestMapping("/outSideUserGetOrderInfoById.aspx")
	public ModelAndView outSideUserGetOrderInfoById(HttpServletRequest request,
													HttpServletResponse response) throws Exception {
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderListService.outSideUserGetOrderInfoById(map);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}

	//移动技师获取工单详情
	@RequestMapping("/getOrderInfo.aspx")
	public ModelAndView getOrderInfo(HttpServletRequest request,
											   HttpServletResponse response) throws Exception {
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			map.put("request", request);
			return workOrderListService.getOrderInfo(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);
		}
		return null;
	}



	public static void main(String[] args) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("a", "");
		System.out.println("1:"+MapUtils.getString(map, "a",""));
		System.out.println("2:"+MapUtils.getString(map, "b",""));
	}



	/**
	 * 我的保修时间轴
	 * @param request
	 * @param response
	 * @return
	 * @throws JSONException
	 */
	@RequestMapping("/workOrderFlowTime.aspx")
	public ModelAndView workOrderFlowTime(HttpServletRequest request,HttpServletResponse response) throws JSONException {
		try {
			String eAppVersion = request.getHeader("EAppVersion");
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			map.put("eAppVersion", eAppVersion);
			return workOrderListService.workOrderFlowTime(map);
		} catch (Exception e) {
			// TODO: handle exception
			ParameterTool.writeErrorResponse(response);
			return null;
		}
	}

	@RequestMapping("/getWorkOrderListForMT.aspx")
	public String getWorkOrderListForMT(HttpServletRequest request,HttpServletResponse response){
		try {
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("request", request);
			map.put("response", response);
			workOrderListService.getWorkListForMT(map);
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e.getMessage(),e);
			ParameterTool.writeErrorResponse(response);
		}
		return null;
	}

    @RequestMapping("/getWorkOrderIsExistence.aspx")
    public String getWorkOrderIsExistence(@RequestParam("OrderNo") String OrderNo, @RequestParam("OrderChannel") String OrderChannel, HttpServletRequest request, HttpServletResponse response) {
        try {
            if (StringUtils.isEmpty(OrderNo)) {
                ParameterTool.writeResponse(response, "请输入工单号");
            }
            if (StringUtils.isEmpty(OrderChannel)) {
                ParameterTool.writeResponse(response, "请输入市场类型");
            }
            Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
            map.put("request", request);
            map.put("response", response);
            workOrderListService.getWorkOrderIsExistence(map);
        } catch (Exception e) {
            // TODO: handle exception
            log.error(e.getMessage(), e);
            ParameterTool.writeErrorResponse(response);
        }
        return null;
    }

}
